package com.pedro.streamer.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.pedro.streamer.R;
import com.pedro.streamer.manager.VideoArrManager;
import com.pedro.streamer.model.ImageObjectFilterModel;
import com.pedro.streamer.utils.FileUtils;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class StickerAdapter extends RecyclerView.Adapter<StickerAdapter.StickerViewHolder> {
    private String TAG = "com.bmi.vcam";
    private VideoArrManager videoArrManager = VideoArrManager.shareInstance();
    public interface OnStickerItemClick {

        void onItemClick(Integer position);
        void onChoosePic(Integer position,boolean isSelect,ImageView imageView);
    }
    private List<String> imgSourceList;
    private OnStickerItemClick mOnStickerItemClick;

    public StickerAdapter() {
        this.imgSourceList = new ArrayList<>();
    }

    public void setOnStickerItemClick(OnStickerItemClick onStickerItemClick) {
        this.mOnStickerItemClick = onStickerItemClick;
    }

    public void setStickerList(List<String> imgSourceList) {
        this.imgSourceList = imgSourceList;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public StickerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sticker_item_view, parent, false);
        return new StickerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull StickerViewHolder holder, int position) {

        holder.stickerImg.setImageBitmap(FileUtils.loadImageFromLocal(imgSourceList.get(position)));
        holder.chooseImage.setSelected(false);
         for (ImageObjectFilterModel imageObjectFilterModel : videoArrManager.getImageObjectFilterRenders()){
             if (imgSourceList.get(position).equals(imageObjectFilterModel.getImagePaths())){
                 holder.chooseImage.setSelected(true);
             }
         }
        holder.chooseLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mOnStickerItemClick != null) {
                    if (holder.chooseImage.isSelected()){
                        holder.chooseImage.setSelected(false);
                    }else {
                        holder.chooseImage.setSelected(true);
                    }
                    mOnStickerItemClick.onChoosePic(position,holder.chooseImage.isSelected(),holder.chooseImage);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return imgSourceList.size();
    }

    static class StickerViewHolder extends RecyclerView.ViewHolder {

        public ImageView stickerImg,chooseImage,moreImage;
        public ConstraintLayout chooseLayout;

        public StickerViewHolder(@NonNull View itemView) {
            super(itemView);
            stickerImg = itemView.findViewById(R.id.sticker_img);
            chooseImage = itemView.findViewById(R.id.choose_image_view);
//            moreImage = itemView.findViewById(R.id.more_image_view);
            chooseLayout = itemView.findViewById(R.id.choose_layout);
        }
    }



}
